// 引入request函数
import request from "@/utils/request";
//引入类型
import type { SpuPageListParamsModel, SpuModel, SpuListModel, BaseSaleAttrListModel, SpuImageListModel, SpuSaleAttrListModel } from "./model/spuModel";

// 定义枚举类型的接口地址
enum API {
  // 获取基础销售属性对象数组的地址
  GetBaseSaleAttrList = "/admin/product/baseSaleAttrList",
  // 删除spu的地址
  DeleteSpuById = "/admin/product/deleteSpu",
  // 根据关键字查找spu列表数据
  GetSpuInfoListByKeyword = "/admin/product/findSpuInfoByKeyword",
  // 根据id找spu对象的地址
  GetSpuInfoBySpuId = "/admin/product/getSpuInfo",
  //根据id数组获取spu列表的地址
  GetSpuInfoListBySpuIds = "/admin/product/inner/findSpuInfoBySpuIdList",
  // 保存spu对象的地址
  AddSpuInfo = "/admin/product/saveSpuInfo",
  // 修改spu对象的地址
  UpdateSpuInfo = "/admin/product/updateSpuInfo",
  // 分页的方式获取spu列表的地址
  GetSpuInfoList = "/admin/product",
  // 获取spu中的图片列表数据的地址
  GetSpuInfoImageList = "/admin/product/spuImageList",
  // 获取spu中的销售属性列表数据的地址
  GetSpuInfoSaleAttrList = "/admin/product/spuSaleAttrList",
}

// 获取基础销售属性对象数组的地址
export const reqGetBaseSaleAttrList = () => request.get<any, BaseSaleAttrListModel>(API.GetBaseSaleAttrList);
// 删除spu的地址
export const reqDeleteSpuById = (spuId: number) => request.delete<any, null>(API.DeleteSpuById + `/${spuId}`);
// 根据关键字查找spu列表数据
export const reqGetSpuInfoListByKeyword = (keyword: string) => request.get<any, SpuListModel>(API.GetSpuInfoListByKeyword + `/${keyword}`);
// 根据id找spu对象的地址
export const reqGetSpuInfoBySpuId = (spuId: number) => request.get<any, SpuModel>(API.GetSpuInfoBySpuId + `/${spuId}`);
//根据id数组获取spu列表的地址
export const reqGetSpuInfoListBySpuIds = (ids: number[]) => request.post<any, SpuListModel>(API.GetSpuInfoListBySpuIds, ids);
// 保存spu对象的地址
export const reqAddSpuInfo = (spuInfo: SpuModel) => request.post<any, null>(API.AddSpuInfo, spuInfo);
// 分页的方式获取spu列表的地址
export const reqGetSpuInfoList = ({ page, limit, category3Id }: SpuPageListParamsModel) => request.get<any, SpuListModel>(API.GetSpuInfoList + `/${page}/${limit}`, { params: { category3Id } });
// 获取spu中的图片列表数据的地址
export const reqGetSpuInfoImageList = (spuId: number) => request.get<any, SpuImageListModel>(API.GetSpuInfoImageList + `/${spuId}`);
// 获取spu中的销售属性列表数据的地址
export const reqGetSpuInfoSaleAttrList = (spuId: number) => request.get<any, SpuSaleAttrListModel>(API.GetSpuInfoSaleAttrList + `/${spuId}`);
// 修改spu对象的地址
export const reqUpdateSpuInfo = (spuInfo: SpuModel) => request.post<any, null>(API.UpdateSpuInfo, spuInfo);

export const reqAddOrUpdate = (spuInfo: SpuModel) => {
  if (spuInfo.id) {
    return request.post<any, null>(API.UpdateSpuInfo, spuInfo);
  } else {
    return request.post<any, null>(API.AddSpuInfo, spuInfo);
  }
};
